from django.shortcuts import render
from .models import Apply
import jwt
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from rest_framework_simplejwt.tokens import RefreshToken
from datetime import datetime
from patient.models import Patient
from doctor.models import Doctor
from appointment.models import Appointment

import os
from django.utils.timezone import now
from django.conf import settings
# Create your views here.


class Get_All(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def get(self, request):
        patient_id = request.GET.get('patient_id')

        if not patient_id:
            return Response({"error": "patient_id is required"}, status=400)

        try:
            # 查找 Patient 实例
            applys = Apply.objects.filter(patient_id=patient_id)

            # applys_data = list(applys.values('id','name','position','description','price','time','status','result','patient_id','doctor_id'))

        except Apply.DoesNotExist:
            return Response({"error": "Patient not found"}, status=400)

        # if applys:
        #     return Response({'applys': applys_data}, status=200)

        if applys:
            applys_data = []
            for apply in applys:
                if apply.status != "已退费":
                    apply_data = {
                        'id': apply.id,
                        'name': apply.name,
                        'position': apply.position,
                        'description': apply.description,
                        'price': apply.price,
                        'time': apply.time,
                        'status': apply.status,
                        'pay_status': apply.pay_status,
                        'result': apply.result,
                        'patient_id': apply.patient_id.id,
                        'patient_name': apply.patient_id.name,
                        'doctor_id': apply.doctor_id.id,
                        'doctor_name': apply.doctor_id.doctor_name,
                        'appointment_id': apply.appointment.id,
                    }
                    applys_data.append(apply_data)

            return Response({'applys': applys_data})

        return Response({"error": "apply not found"}, status=500)


class Get_Target(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def get(self, request):
        apply_id = request.GET.get('apply_id')

        if not apply_id:
            return Response({"error": "apply_id is required"}, status=400)

        try:

            apply = Apply.objects.get(id=apply_id)
        except Apply.DoesNotExist:
            return Response({"error": "apply not found"}, status=400)

        if apply:

            return Response({'apply_id': apply.id,
                             'apply_name': apply.name,
                             'apply_position': apply.position,
                             'apply_description':apply.description,
                             'apply_price':apply.price,
                             'pay_status': apply.pay_status,
                             'apply_time':apply.time,
                             'apply_status':apply.status,
                             'apply_result':apply.result,
                             'apply_patient_id':apply.patient_id.id,
                             'apply_patient_name': apply.patient_id.name,
                             'apply_doctor_id':apply.doctor_id.id,
                             'apply_doctor_name': apply.doctor_id.doctor_name,
                             'appointment_id': apply.appointment.id
                             }, status=200)

        return Response({"error": "apply not found"}, status=400)

class Change(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def post(self, request):
        apply_id = request.data.get('apply_id')
        apply_name = request.data.get('apply_name')
        apply_position = request.data.get('apply_position')
        apply_description = request.data.get('apply_description')
        apply_price = request.data.get('apply_price')
        apply_result = request.data.get('apply_result')


        if not apply_name:
            return Response({"error": "apply_name is required"}, status=400)
        if not apply_description:
            return Response({"error": "apply_description is required"}, status=400)
        if not apply_position:
            return Response({"error": "apply_position is required"}, status=400)
        if not apply_price:
            return Response({"error": "apply_price is required"}, status=400)
        if not apply_result:
            return Response({"error": "apply_result is required"}, status=400)

        if not apply_id:
            return Response({"error": "apply_id is required"}, status=400)

        try:

            apply = Apply.objects.get(id=apply_id)
        except Apply.DoesNotExist:
            return Response({"error": "Apply not found"}, status=400)
        if apply_position and apply_name and apply_description :
            apply.position = apply_position
            apply.description = apply_description
            apply.price = apply_price
            apply.name = apply_name
            apply.result = apply_result
            apply.save()
            return Response({"message": "Success"}, status=200)

        return Response({"error": "Unsuccess"}, status=400)



class Create(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def post(self, request):
        # apply_name = request.data.get('apply_name')
        # apply_position = request.data.get('apply_position')
        # apply_description = request.data.get('apply_description')
        apply_appointment = request.data.get('appointment_id')
        # apply_patient_id = request.data.get('apply_patient_id')
        # apply_doctor_id = request.data.get('apply_doctor_id')
        patient_instance = Appointment.objects.get(id=apply_appointment).patient
        doctor_instance = Appointment.objects.get(id=apply_appointment).doctor
        apply_patient_id = patient_instance.id
        apply_doctor_id = doctor_instance.id
        appointment_instance = Appointment.objects.get(id=apply_appointment)
        # if not apply_name:
        #     return Response({"error": "apply_name is required"}, status=400)
        # if not apply_description:
        #     return Response({"error": "apply_description is required"}, status=400)
        # if not apply_position:
        #     return Response({"error": "apply_position is required"}, status=400)
        if not apply_patient_id:
            return Response({"error": "apply_patient_id is required"}, status=400)
        if not apply_doctor_id:
            return Response({"error": "apply_doctor_id is required"}, status=400)
        if not apply_appointment:
            return Response({"error": "appointment is required"}, status=400)

        if apply_doctor_id and apply_patient_id :
            apply = Apply(patient_id=patient_instance, doctor_id=doctor_instance,appointment=appointment_instance)
            apply.save()
            return Response({"message": "创建成功"}, status=200)

        return Response({"error": "Unsuccess"}, status=400)

class Done(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def post(self, request):

        apply_id = request.data.get('apply_id')
        apply_status = request.data.get('apply_status')

        if not apply_id:
            return Response({"error": "apply_id is required"}, status=400)

        if not apply_status:
            return Response({"error": "apply_status is required"}, status=400)


        try:

            apply = Apply.objects.get(id=apply_id)
        except Apply.DoesNotExist:
            return Response({"error": "apply not found"}, status=500)
        if apply.status == "已退费":
            return Response({"error": "已退款无法完成检验"}, status=400)
        if apply.pay_status == "pending":
            return Response({"error": "未缴费无法完成检验"}, status=400)


        if apply_id and apply_status:

            apply.status = apply_status
            apply.save()

            return Response({"message": "Success"}, status=200)
        return Response({"error": "Unsuccess"}, status=500)

# class Create(APIView):
#     permission_classes = [AllowAny]  # 允许任何人访问
#
#     def post(self, request):
#         apply_name = "123"
#         apply_position = "123"
#         apply_description = "123"
#
#         apply_patient_id = 1
#         apply_doctor_id = 1
#
#
#         if not apply_name:
#             return Response({"error": "apply_name is required"}, status=400)
#         if not apply_description:
#             return Response({"error": "apply_description is required"}, status=400)
#         if not apply_position:
#             return Response({"error": "apply_position is required"}, status=400)
#         if not apply_patient_id:
#             return Response({"error": "apply_patient_id is required"}, status=400)
#         if not apply_doctor_id:
#             return Response({"error": "apply_doctor_id is required"}, status=400)
#
#         if apply_position and apply_name and apply_doctor_id and apply_patient_id and apply_description:
#             Apply.objects.create(name=apply_name,
#                                  position=apply_position,
#                                  description=apply_description,
#                                  patient_id=apply_patient_id,
#                                  doctor_id=apply_doctor_id
#                                  )
#             return Response({"message": "创建成功"}, status=200)
#
#         return Response({"error": "Unsuccess"}, status=400)